package org.example.thread.threadException;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @Auther:Liu
 * @Testname:App
 * @Date:2025/8/14 14:56
 */
public class App {

    public static void a() {
        DemoA d = new DemoA();
        Thread t1 = new Thread(d::a);
        Thread t2 = new Thread(d::a);
        t1.start();
        t2.start();
    }

    @SneakyThrows
    public static void b() {
        DemoB d = new DemoB();
        new Thread(d::a1).start();
        new Thread(d::a1).start();
        Thread.sleep(3000);
        new Thread(d::a3).start();
    }


    public static void c( ){
        DemoC d = new DemoC();
        new Thread(d::c).start();
        new Thread(d::c).start();
        new Thread(d::c).start();
    }

    @SneakyThrows
    public static void d(){
        DemoD d = new DemoD();
        Thread t = new Thread(d::jisuan);
        t.start();
        t.join(2100);
        int sum = d.sum();
        System.out.println(sum);
    }


    public static void e(){
        Object lock1 = new Object();
        Object lock2 = new Object();
        Thread t1 = new Thread(()->{
            synchronized (lock1){
                System.out.println("A获取到了锁A");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock2){
                    System.out.println("A获取到了锁B");
                }
            }


        });
        t1.start();
        Thread t2 = new Thread(()->{
            synchronized (lock1){
                System.out.println("B获取到了锁A");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock2){
                    System.out.println("B获取到了锁B");
                }
            }

        });
        t2.start();
    }

    public static void main(String[] args) {
        e();
    }
}
